package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/travel")
public class TravelController {
    @Autowired
    private ITravelService travelService;

    /**
     * 列表查询
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")TravelQuery qo){
            qo.setDifference(0);
            Page<Travel> page = travelService.query(qo);
            model.addAttribute("page", page);
        return "travel/list";
    }

    /**
     * 查看游记内容
     * @param id
     * @return
     */
    @RequestMapping("/getContentById")
    @ResponseBody
    public Object getContentById(String id){
        Travel travel = travelService.findById(id);
        return JsonResult.success(travel.getContent());
    }

    /**
     * 改变当前文章状态
     * @return
     */
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(String id, int state){
        travelService.changeState(id, state);
        return JsonResult.success();
    }
}
